#============================================================
#	Node List
#============================================================
# @datetime: 2022-5-3 15:49:35
#============================================================
tool
extends VBoxContainer
class_name _CodeEditor_Scene_NodeList


signal added_node(node)
signal removed_node(node)


export var create_node : PackedScene

onready var _v_box_container = get_node("VBoxContainer") as VBoxContainer
onready var _add_button = get_node("AddButton")


#============================================================
#   Set/Get
#============================================================
func get_node_list() -> Array:
	return _v_box_container.get_children()


#============================================================
#   内置
#============================================================
func _ready():
	add_one()
	if not _add_button.is_connected("pressed", self, "_on_AddButton_pressed"):
		_add_button.connect("pressed", self, "_on_AddButton_pressed")


#============================================================
#   自定义
#============================================================
func add_one() -> Node:
	if create_node:
		var node : Node = create_node.instance()
		node.connect("tree_exiting", self, "_on_Node_tree_exiting", [node])
		_v_box_container.add_child(node)
		emit_signal("added_node", node)
		var lineedit := node.get_line_edit() as LineEdit
		lineedit.connect("text_entered", self, "_line_edit_pressed_enter")
		return node
	else:
		printerr("没有设置创建的节点")
	return null


func clear():
	for child in get_node_list():
		child.queue_free()


#============================================================
#   连接信号
#============================================================
func _on_AddButton_pressed():
	var node = add_one()
	if node.has_method("get_line_edit"):
		var lineedit : LineEdit = node.get_line_edit()
		lineedit.grab_focus()
	else:
		printerr("添加失败")

func _line_edit_pressed_enter(_text):
	_on_AddButton_pressed()

func _on_Node_tree_exiting(node: Node):
	emit_signal("removed_node", node)

